{% extends 'layout/layout.html' %}
{% import 'libs/libs.html' as libs %}
{% import 'libs/const.html' as const %}
{% import 'common/js.html.j2' as common_js%}
{% block search_body %}
{%
    set form_config = [
        {'label':'标题', 'name':'title',},
    ]
%}

{{ libs.build_search_form(form_config) }}
{% endblock %}

{% block data_body %}
<table class="table table-hover">
    <thead>
        <tr>
            <th>消息id</th>
            <th>标题</th>
            <th>内容</th>
            <th>添加时间</th>
            <th>更新时间</th>
            <th>编辑</th>
            <th>移除</th>
        </tr>
    </thead>
    <tbody>
    {% for m in message_list %}
        <tr>
            <td>{{ m.sm_id }}</td>
            <td>{{ m.title }}</td>
            <th>{{ m.content|striptags|truncate(18, Ture) }}</th>
            <td>{{ m.add_time|format_timestamp }}</td>
            <td>{{ m.update_time|format_timestamp }}</td>
            <td><a href="javascript:;" onclick="javascript:message_edit({{ m.sm_id }}, '{{ m.title }}', '{{ m.content }}', this);">编辑</a></td>
            <td><a href="javascript:;" onclick="javascript:delete_list_data('确定要移除该消息吗？', '/sys/message_delete',{'sm_id':{{m.sm_id}}},this);">移除</a></td>
        </tr>
    {% endfor %}
    </tbody>
</table>

{{ libs.pagination_html(pagination) }}
<!-- 编辑系统消息弹出层 -->
<div class="modal fade" id="message_modal" tabindex="-1" role="dialog"  aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" style="margin-top: 10%;width: 150%;">
            <form class="form-horizontal" id="message_modal_form" autocomplete="off" enctype="multipart/form-data">
                <input type="hidden" name="sm_id" id="sm_id" value="">
                <input type="hidden" name="save_type" id="save_type" value="">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span>
                        <span class="sr-only">关闭</span>
                    </button>
                    <h4 class="modal-title">编辑系统消息</h4>
                </div>
                <div class="modal-body">
                    {{ libs.input('标题', 'title', '', required='required') }}
                    {{ libs.textarea('内容', 'content', '', required='required') }}
                </div>
                <div class="modal-footer">
                    <a type="button" class="btn btn-primary" id="submit">提交</a>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block footer %}
<script type="text/javascript">
function message_edit(sm_id, title, content, adom) {
    $('#sm_id').val(sm_id);
    $('input[id=title]').eq(1).val(title);
    $('#content').val(content);
    $('#save_type').val('2');
    $('#message_modal').modal({backdrop:false});
}
$(document).ready( function(){
    $("#add_new").click(function(){
        $('#sm_id').val('');
        $('input[id=title]').eq(1).val('');
        $('#content').val('');
        $('.modal-title').html('新增系统消息');
        $('#save_type').val('1');
        $('#message_modal').modal({backdrop:false});
    });
    $('a#submit').click(function(){
        var sm_id = $('#sm_id').val();
        var title = $('input[id=title]').eq(1).val();
        var content = $('#content').val();
        var save_type = $('#save_type').val();
        if (title == ''){
          window.wxc.xcConfirm('标题不能为空', window.wxc.xcConfirm.typeEnum.info);
          return false;
        }
        if (content == ''){
          window.wxc.xcConfirm('内容不能为空', window.wxc.xcConfirm.typeEnum.info);
          return false;
        }
        if (save_type == '1'){
            msg = '新增成功！';
        }else{
            msg = '修改成功！'
        }
        $.get('/sys/message_save', {'sm_id':sm_id, 'title':title, 'content':content, 'save_type':save_type}, function (res){
            if (res != 'ok') {
                window.wxc.xcConfirm(res, window.wxc.xcConfirm.typeEnum.error);
                return false;
            } else {
                window.wxc.xcConfirm(msg, window.wxc.xcConfirm.typeEnum.success);
                var t1 = setTimeout(function(){
                    window.location.href = '';
                }, 800);
            }
        });
    });
});
</script>
{{ common_js.delete_list_data()}}
{% endblock %}
